home *** CD-ROM | disk | FTP | other *** search
- Listing 4. Filter with circular buffer
-
- /* Filters the vector, v, of input values, putting the
- * results back in the same vector.
- */
- void filter(Filter *f, double *v, int length)
- {
- /* Making a local copy saves execution time */
- CircularBuffer y = *(f->y);
-
- /* Loop through the vector values */
- for(; length--; v++)
- {
- double
- sum = *v, /* becomes filter output */
- *c = f->coef;
- int k = f->order, /* The number of coefficients */
- i = y.index; /* Index to previous outputs */
-
- /* Loop through filter coefficients */
- for(; k--; c++)
- {
- i = (i - 1) & y.mask;
- sum -= *c * y.buffer[i];
- }
- /* Save output for next data point */
- y.buffer[y.index] = sum;
- y.index = (y.index + 1) & y.mask;
-
- *v = sum; /* Set output value */
- }
- /* Store the local copy back in the filter structure */
- *(f->y) = y;
- }
-
-
-